package com.jeegot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeegot.aspect.Log;
import com.jeegot.entity.User;
import com.jeegot.entity.UserOpenid;
import com.jeegot.mapper.UserMapper;
import com.jeegot.service.IUserOpenidService;
import com.jeegot.service.IUserService;
import com.jeegot.utils.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author wjun_java@163.com
 * @since 2021-02-14
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IUserOpenidService userOpenidService;

    @Override
    @Log
    public void updateUserLoginTime(String loginAccount) {

        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.eq(User::getLoginAccount, loginAccount);
        User user = getOne(queryWrapper);
        if(user != null){
            user.setLoginTime(LocalDateTime.now());
            updateById(user);
        }

    }

    @Override
    public void updateUserPassword(User userParam) throws Exception {

        User user = getById(userParam.getId());
        if(user == null) {
            throw new Exception("用户不存在");
        }

        if(StringUtils.isBlank(userParam.getPassword())) {
            throw new Exception("请输入旧密码");
        }

        if(StringUtils.isBlank(userParam.getNewPassword())) {
            throw new Exception("请输入新密码");
        }

        if(StringUtils.isBlank(userParam.getNewConfirmPassword())) {
            throw new Exception("请再次输入新密码");
        }

        if(!userParam.getNewPassword().equals(userParam.getNewConfirmPassword())) {
            throw new Exception("两次新密码输入不一致");
        }

        final String result = PasswordUtils.getMd5Password(user.getSalt(), userParam.getPassword());
        if(!result.equals(user.getPassword())) {
            throw new Exception("旧密码输入错误");
        }

        final String salt = System.currentTimeMillis() + "";
        final String newPwd = PasswordUtils.getMd5Password(salt, userParam.getNewPassword());

        user.setPassword(newPwd);
        user.setSalt(salt);
        updateById(user);

    }

    @Override
    public void initSuperAdmin() {
        getBaseMapper().initSuperAdminRole();
        getBaseMapper().initSuperAdminUser();
        getBaseMapper().initSuperAdminUserRole();
    }

    @Override
    @Transactional
    public User saveUserOfOpenid(String openid, String unionId, String nickName, String avatarUrl, String type) throws Exception {
        UserOpenid userOpenid = userOpenidService.getByOpenid(openid, UserOpenid.TYPE_WECHAT_MINI);
        User user;
        if(userOpenid == null) {
            user = new User();
            user.setNickName(nickName);
            user.setHeadImg(avatarUrl);
            user.setSource(User.SourceType.WX_MINI_PROGRAM.name().toLowerCase());
            save(user);
            userOpenid = new UserOpenid();
            userOpenid.setUserId(user.getId());
            userOpenid.setType(type);
            userOpenid.setValue(openid);
            userOpenid.setUnionId(unionId);
            userOpenidService.save(userOpenid);
        }else {
            user = getById(userOpenid.getUserId());
        }
        return user;
    }

}
